/*
 * @# UserCirclesServiceImpl.java 2011-12-22 上午11:15:42
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ianswer.common.orm.PublicDao;
import com.ianswer.common.util.time.SecondsUtils;
import com.ianswer.px.user.entity.Circle;
import com.ianswer.px.user.service.CircleService;
import com.ianswer.px.user.service.FollowCircleService;

/**
 * @author pananz
 */
@Service("circleService")
public class CircleServiceImpl implements CircleService {

	@Resource(name = "publicDao")
	private PublicDao publicDao;

	@Resource(name = "followCircleService")
	private FollowCircleService followCircleService;

	@Override
	public Long insert(Circle circle) {
		return (Long) publicDao.insert("circles.insert", circle);
	}

	@Override
	public Long insertList(String[] circles, Long userId) {
		Long lastCircleId = null;
		for (String circleName : circles) {
			if (get(circleName, userId) != null) {
				continue;
			}
			Circle circle = new Circle();
			circle.setCircleName(circleName);
			circle.setCreatedAt(SecondsUtils.seconds());
			circle.setUserId(userId);
			lastCircleId = insert(circle);
		}
		return lastCircleId;
	}

	@Override
	public void update(Circle circle) {
		publicDao.update("circles.update", circle);
	}

	@Override
	public void delete(Long id) {
		publicDao.delete("circles.delete", id);
		followCircleService.deleteByCircleId(id);
	}

	@Override
	public Circle get(Long id) {
		return publicDao.querySingle("circles.getCirclesById", id);
	}

	@Override
	public List<Circle> query(Long userId) {
		return publicDao.query("circles.queryCirclesByUserId", userId);
	}

	private Circle get(String circleName, Long userId) {
		Circle circle = new Circle();
		circle.setUserId(userId);
		circle.setCircleName(circleName);
		return publicDao.querySingle("circles.getCirclesByName", circle);
	}

	@Override
	public List<Circle> queryCircleByFollowId(Long followId) {
		return publicDao.query("circles.queryCircleByFollowId", followId);
	}

}
